//加载字典
function dictJudge(value, options, row){
    //sex为自定义字典项
	var statusObj = vm.options.sex;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}
function showPhoto(id){
    if(id == null){
        return ;
    }
    vm.title = '查看照片';
    $('#showPhotoModal').modal('show');

    $.get(baseURL + "cgs/cgspolicepost/info/"+id, function(r){
        vm.cgsPolicePost = r.cgsPolicePost;

        $("#idCard_01").attr("src",vm.cgsPolicePost.scanUrlZ);
        $("#idCard_01").attr("href",vm.cgsPolicePost.scanUrlZ);
        $("#idCard_02").attr("src",vm.cgsPolicePost.scanUrlF);
        $("#idCard_02").attr("href",vm.cgsPolicePost.scanUrlF);

    });

}

$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'cgs/cgspolicepost/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true , hidden:true},
			{ label: '业务名称', name: 'businessName', index: 'businessName', width: 80 },

			{ label: '用户姓名', name: 'userName', index: 'userName', width: 80 },
			{ label: '联系方式', name: 'mobile', index: 'mobile', width: 80 }, 			
			{ label: '身份证号', name: 'idCardNum', index: 'idCardNum', width: 80 },
			{ label: '车牌号码', name: 'vehicleNum', index: 'vehicleNum', width: 80 },
			{ label: '订单号', name: 'orderNum', index: 'orderNum', width: 80 },
			{ label: '工本费', name: 'nominalFee', index: 'nominalFee', width: 80 },
			{ label: '邮寄费', name: 'postFee', index: 'postFee', width: 80 },
			{ label: '总费用', name: 'totalPrice', index: 'totalPrice', width: 80 },
			{ label: '订单状态', name: 'status', index: 'status', width: 80 },
			{ label: '支付方式', name: 'payType', index: 'payType', width: 80 },
			{ label: '快递单号', name: 'expressNo', index: 'expressNo', width: 80 },
			{ label: '创建时间', name: 'createTime', index: 'createTime', width: 80 },
			{ label: '身份证照片', name: '', index: '', width: 80,
                formatter: function (cellvalue, option, rowObject) {
                    return '&nbsp;&nbsp;<button class="btn btn-info btn-xs" type="button" onclick=showPhoto(\"' + rowObject.id + '\")>查看</button>';
                }
            }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        multiboxonly: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

    //初始化日期范围组件
    $('#datepicker_range input').datepicker({
        language: 'zh-CN',//显示中文
        startView: 0, //选择器开始界面 0：天（默认 1：月2：年3：十年 4：世纪
        minViewMode: 0,//最小选择精度 0：天（最小默认）
        maxViewMode: 2, //最大选择精度 4：世纪（最大默认）
        format: 'yyyy-mm-dd',//显示格式
        minView: "day",//设置只显示到月份
        // initialDate: new Date(),
        autoclose: true,//选中自动关闭
        // todayBtn: true//显示今日按钮
        clearBtn: true //清除按钮
        //todayHighlight: true //今日高亮
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
	    q:{
            businessName:'',
            userName:'',
            mobile:'',
            status:''
        },
		showList: true,
		title: null,
		cgsPolicePost: {},
        statusObject:{} //办理状态集合
	},
    mounted:function(){
        this.$nextTick(function(){ this.initSysDict()});
    },
	methods: {
        initSysDict:function() {
            $.get(baseURL + "sys/dict/select/policePostStatus", function (r) {
                if (r.code === 0) {
                    vm.statusObject = r.dict;
                } else {
                    console.log(r.msg);
                }
            });
        },
        reset: function(){
            vm.q.businessName = '';
            vm.q.userName = '';
            vm.q.mobile = '';
            vm.q.status = '';
            $("#datepicker_start").val('');
            $("#datepicker_end").val('');

        },
		query: function () {
			vm.reload();
		},
		detail: function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
			vm.title = "办理详情";
            vm.getInfo(id);
            $('#detailModal').modal('show');

        },
        //办理
        policePostUpdate: function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.title = "业务办理";
            vm.getInfo(id);
            $('#policePostModal').modal('show');

        },
        //退款审核
        refundCheck: function(){

        },
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.cgsPolicePost.id == null ? "cgs/cgspolicepost/save" : "cgs/cgspolicepost/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.cgsPolicePost),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "cgs/cgspolicepost/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "cgs/cgspolicepost/info/"+id, function(r){
                vm.cgsPolicePost = r.cgsPolicePost;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'businessName': vm.q.businessName,
                    'userName': vm.q.userName,
                    'mobile': vm.q.mobile,
                    'status': vm.q.status,
                    'startTime':$("#datepicker_start").val(),
                    'endTime':$("#datepicker_end").val()
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});